package ubc.master;

import java.util.Date;


public class Main {
   
    public static void main(String[] args) throws InterruptedException{
        
    	int mode = ubc.CrawlWrapper.FULL_MODE;
        int timeout = 30;
        int time = 60;
        int numSend = 20;
        Boolean quit = false;

        String[] bootstrapHostPort;
        String bootstrapHost = "reala.ece.ubc.ca";
        int bootstrapPort = 5627;

        int serverPort = 8000;
                
        // PARSE ARGUMENTS
        if ( args.length > 0 && args[0].equals("help") ) {
            System.out.println("Usage: <port=XX> <-full|-minimal> <timeout=XX> <bootstrapHost:bootstrapPort> <time=XX> <-quit> <numSend>");
            System.out.println("All parameters are optional");
            System.out.println("Default is port=8000 -full timeout=30 reala.ece.ubc.ca:5627 time=60 20");
            System.exit(0);
        }
        try {
            for (int i=0; i<args.length; i++) {
                if( args[i].startsWith("port") )
                    serverPort = Integer.parseInt(args[i].split("=")[1]);
                if(args[i].compareTo("-minimal") == 0)
                    mode = ubc.CrawlWrapper.MINIMAL_MODE;
                if(args[i].compareTo("-full") == 0)
                    mode = ubc.CrawlWrapper.FULL_MODE;
                if(args[i].compareTo("-quit") == 0)
                    quit = true;
                if(args[i].contains(":")) {
                    bootstrapHostPort = args[i].split(":");
                    bootstrapHost = bootstrapHostPort[0];
                    bootstrapPort = Integer.parseInt(bootstrapHostPort[1]);
                }
                if(args[i].startsWith("timeout"))
                    timeout = Integer.parseInt(args[i].split("=")[1]);
                if(args[i].startsWith("time"))
                    time = Integer.parseInt(args[i].split("=")[1]);
                if(args.length>1 && i == args.length-1)
                    numSend = Integer.parseInt(args[i]);
            }
        } catch (Exception e) {
            System.out.println("Usage: <port=XX> <-full|-minimal> <timeout=XX> <bootstrapHost:bootstrapPort> <time=XX> <-quit> <numSend>");
            System.out.println("All parameters are optional");
            System.out.println("Default is port=8000 -full timeout=30 reala.ece.ubc.ca:5627 time=60 20");
            System.exit(0);
        }
        System.out.println("ARGUMENTS:");
        System.out.println("ServerPort: " + serverPort);
        if ( mode == ubc.Constants.FULL_MODE )
            System.out.println("Mode      : " + "Full");
        if ( mode == ubc.Constants.MINIMAL_MODE )
            System.out.println("Mode      : " + "Minimal");
        System.out.println("Timeout   : " + timeout);
        System.out.println("Bootstrap : " + bootstrapHost + ":" + bootstrapPort);
        System.out.println("Run Time  : " + time);
        System.out.println("Send Quit : " + quit);
        System.out.println("Num Send  : " + numSend);
        System.out.println();
                
        // START THE SERVER
        try {
            Server ms = new Server(bootstrapHost, bootstrapPort, serverPort, mode, numSend, time);
            ms.start();
//            ms.join();
            // Kill the Master once 'time' seconds have passed
//            while (true) {
//                //mct.join();
//                if (((System.currentTimeMillis() - startTime) > time*1000)) {
//                    System.out.println("\nProgram timeout reached");
//                    endDate = new Date();
//                    ms.interrupt();
//                    ms.join();
//                    break;
//                } else {
////                    System.out.println( "Time: " + (System.currentTimeMillis() - startTime)/1000 );
//                    Thread.sleep(1000);
//                }
//            }

//            System.out.println("Aggregating Stats...");
//            Aggregate agg = new Aggregate(ubc.Constants.serverFilePath, mode, time, startDate, endDate);
//            System.out.println("Done!");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
            
    }

}
